package com.huya.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.huya.common.result.PageResult;
import com.huya.mapper.KnowledgeMapper;
import com.huya.pojo.dto.KnowledgeDTO;
import com.huya.pojo.dto.KnowledgePageQueryDTO;
import com.huya.pojo.entity.Knowledge;
import com.huya.service.KnowledgeService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class KnowledgeServiceImpl implements KnowledgeService {
    @Autowired
    private KnowledgeMapper knowledgeMapper;

    /**
     * 添加知识
     * @param knowledgeDTO
     */
    public void add(KnowledgeDTO knowledgeDTO) {
        Knowledge knowledge = new Knowledge();
        BeanUtils.copyProperties(knowledgeDTO, knowledge);
        knowledge.setCreateTime(LocalDateTime.now());
        knowledgeMapper.insert(knowledge);
    }

    /**
     * 批量删除
     * @param ids
     */
    public void delete(List<Long> ids) {
        knowledgeMapper.deleteByIds(ids);
    }

    /**
     * 分页查询
     * @param knowledgePageQueryDTO
     * @return
     */
    public PageResult pageQuery(KnowledgePageQueryDTO knowledgePageQueryDTO) {
        PageHelper.startPage(knowledgePageQueryDTO.getPage(), knowledgePageQueryDTO.getPageSize());
        Page<Knowledge> page = knowledgeMapper.pageQuery(knowledgePageQueryDTO);
        return new PageResult(page.getTotal(), page.getResult());
    }


    /**
     * 根据id查询
     * @param id
     * @return
     */
    public Knowledge getById(Long id) {
        return knowledgeMapper.getById(id);
    }


    /**
     * 更新知识
     * @param knowledgeDTO
     */
    public void update(KnowledgeDTO knowledgeDTO) {
        Knowledge knowledge = new Knowledge();
        BeanUtils.copyProperties(knowledgeDTO, knowledge);
        knowledgeMapper.update(knowledge);
    }
}
